package com.etc.admin.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.etc.admin.service.UserService;
import com.etc.admin.util.PageData;
import com.etc.entity.User;
import com.etc.user.service.UsersService;
import com.etc.util.CommonMessage;
import com.etc.util.CountMessage;

@Controller
@RequestMapping("userapi")
public class UserController {

	@Resource(name = "userService")
	private UserService userService;

	@Resource(name = "usersService")
	private UsersService usersService;

	@RequestMapping(value = "user", method = RequestMethod.GET)
	@ResponseBody
	public PageData<User> getUserByPage(Integer page, Integer limit, String content) {
		if (content == null) {
			content = "";
		}
		/* System.out.println(content); */
		PageData<User> data = userService.selectUserByPage(page, limit, content);
		return data;
	}

	@RequestMapping(value = "user", method = RequestMethod.PUT)
	@ResponseBody
	public CommonMessage updateUser(@RequestBody User user) {

		CommonMessage cm = new CommonMessage();

		boolean flag = userService.updateByPrimaryKeySelective(user);
		if (flag) {
			cm.setMsg("修改成功");
		} else {
			cm.setMsg("修改失败");
		}
		return cm;
	}

	/**
	 * 用户总数（李添杭）
	 * 
	 * @return
	 */
	@RequestMapping(value = "getUserCount", method = RequestMethod.GET)
	@ResponseBody
	public CountMessage getUserCount() {
		CountMessage cm = new CountMessage();
		int userCount = userService.userCount();
		cm.setUserCount(userCount);

		/* System.out.println(userCount); */
		return cm;
	}

}
